﻿using UnityEngine;
using System.Collections;
using SevenZip;
using System.IO;
using UnityEditor;
using System;
using System.Threading;
using UnityEngine.UI;
using System.Collections.Generic;
public class Lzma_demo : MonoBehaviour {
	[SerializeField] Slider m_Slider;
	public Text m_text;
	CodeProgress m_CodeProgress = null; //进度;
	Thread m_PackUPKThread = null; //UPK打包线程;
	Thread m_CompressThread = null; //压缩线程;
	Thread m_DecompressThread = null; //解压缩线程;
	Thread m_ExtraUPKThread = null; //释放UPK文件线程;
	string ApplicationdataPath = string.Empty;
	float m_Percent = 0f;
	string m_PercentText;
	// Use this for initialization
	void Start() {
		ApplicationdataPath = Application.dataPath + "/../lzma-test";
		m_CodeProgress = new CodeProgress(SetProgressPercent);
	}
	void Update() {
		if (m_Percent > 0f) {
			m_Slider.value = m_Percent;
			m_text.text = m_PercentText;
			if (m_Percent == 1f) {
				Debug.Log("m_Percent==1f");
				m_Percent = 0f;
				AssetDatabase.Refresh();
			}
		}
	}
	void OnGUI() {
		if (GUI.Button(new Rect(100, 100, 100, 100), "1-UPKPack")) {
			m_Slider.value = 0f;
			m_PackUPKThread = new Thread(new ThreadStart(TestPackUPK));
			m_PackUPKThread.Start();
		}
		if (GUI.Button(new Rect(300, 100, 100, 100), "2-Compress")) {
			m_Slider.value = 0f;
			m_CompressThread = new Thread(new ThreadStart(TestCompress));
			m_CompressThread.Start();
		}
		if (GUI.Button(new Rect(100, 300, 100, 100), "3-DeCompress")) {
			m_Slider.value = 0f;
			m_DecompressThread = new Thread(new ThreadStart(TestDeCompress));
			m_DecompressThread.Start();
		}
		if (GUI.Button(new Rect(300, 300, 100, 100), "4-ExtraUPK")) {
			m_Slider.value = 0f;
			m_ExtraUPKThread = new Thread(new ThreadStart(TestExtraUPK));
			m_ExtraUPKThread.Start();
		}
	}
	void TestPackUPK() {
		try {
			MyPackRes.PackFolder(ApplicationdataPath + "/lib", ApplicationdataPath + "/lib.upk", m_CodeProgress);
		}
		catch (Exception ex) {
			Debug.LogException(ex);
		}
	}
	void TestCompress() {
		try {
			LZMAHelper.Compress(ApplicationdataPath + "/lib.upk", ApplicationdataPath + "/lib.zupk", m_CodeProgress);
		}
		catch (Exception ex) {
			Debug.LogException(ex);
		}
	}
	void TestDeCompress() {
		try {
			LZMAHelper.DeCompress(ApplicationdataPath + "/lib.zupk", ApplicationdataPath + "/lib1.upk", m_CodeProgress);
		}
		catch (Exception ex) {
			Debug.LogException(ex);
		}
	}
	void TestExtraUPK() {
		try {
			UPKExtra.ExtraUPK(ApplicationdataPath + "/lib1.upk", ApplicationdataPath + "/lib1-upkextra", m_CodeProgress);
		}
		catch (Exception ex) {
			Debug.LogException(ex);
		}
	}
	void SetProgressPercent(Int64 fileSize, Int64 processSize) {
		if (fileSize == processSize) {
			m_Percent = 1;
			m_PercentText = $"完成";
		}
		else { 
			m_Percent = (float) processSize / fileSize;
			m_PercentText = $"{processSize} / {fileSize}";
		}
	}
}